package TCP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package test
 * @date 2024/4/7 下午2:13
 */
public class Day09_Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器启动成功");
            while (true) {
                System.out.println("等待客户端连接");
                Socket accept = serverSocket.accept();
                System.out.println(accept.getRemoteSocketAddress() + "上线了");
                //线程池
                Pool1 pool1 = new Pool1(accept);
                Thread thread = new Thread(pool1);
                thread.start();

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

class Pool1 implements Runnable {
    Socket socket;

    public Pool1(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            while (true) {
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                while (true) {
                    String s = bufferedReader.readLine();
                    System.out.println(socket.getRemoteSocketAddress() + "说：" + s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(socket.getRemoteSocketAddress() + "下线了");
            ;
        }
    }
}